<template>
  <el-container class="home_container">
    <!-- 页面头部区域 -->
    <el-header class="home_header">
      <!-- 头部左侧区域 -->
      <div>
        <img src="../assets/heima.png" alt="" />
        <span>黑马电商管理后台</span>
      </div>
      <!-- 头部右侧区域 -->
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'" class="home_aside">
        <div class="toggle_button" @click="toggleCollapse">|||</div>
        <el-menu
          class="el-menu-vertical-demo"
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="default_active"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in Meunlist"
            :key="item.id"
          >
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/' + subitem.path"
              v-for="subitem in item.children"
              :key="subitem.id"
              @click="saveNavState('/' + subitem.path)"
            >
              <i class="el-icon-menu"></i>{{ subitem.authName }}</el-menu-item
            >
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主页区域 -->
      <el-main class="home_main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: 'HomePage',
  data() {
    return {
      Meunlist: [],
      // 一级图标匹配
      iconsObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      isCollapse: false,
      default_active: ''
    }
  },
  // 打开页面就开始获取侧边栏数据
  created() {
    this.getMeun()
    this.default_active = sessionStorage.getItem('defaultActive')
  },
  methods: {
    logout() {
      // 点击退出按钮后，清除token并重定向到登录页面
      sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取侧边栏数据，动态生成侧边栏
    async getMeun() {
      const { data: res } = await this.$http.get('menus')
      console.log(this.$http.defaults.baseURL)
      console.log(res)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.Meunlist = res.data
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavState(index) {
      sessionStorage.setItem('defaultActive', index)
    }
  }
}
</script>

<style lang="less" scoped>
.home_container {
  height: 100%;
}
.home_header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  // 让button上下有空隙
  align-items: center;
  padding-left: 0;
  color: #fff;
  > div {
    display: flex;
    align-items: center;
    > span {
      margin-left: 15px;
    }
  }
}
.home_aside {
  background-color: #333744;
  // 菜单右边框对不齐
  .el-menu {
    border-right: none;
  }
}
.home_main {
  background-color: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggle_button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  text-align: center;
  letter-spacing: 0.2em;
  color: #fff;
  cursor: pointer;
}
</style>>
